package tt.dz.egobus.servicemanager.business.distribute;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import tt.dz.egobus.api.managerservice.manager.RequestTask;
import tt.dz.egobus.api.managerservice.manager.UserRequestTypeInfo;
import tt.dz.egobus.bean.BusinessStatus;
import tt.dz.egobus.servicemanager.bean.AbstractGrabApplicationContext;
import tt.dz.egobus.servicemanager.business.convert.BespeakTimeRedisRestoreConvertor;
import tt.dz.egobus.servicemanager.business.point.BusPoint;
import tt.dz.egobus.servicemanager.core.exception.BusinessException;
import tt.dz.egobus.servicemanager.entities.DriverRegisterBusRelative;
import tt.dz.egobus.servicemanager.service.*;
import tt.dz.egobus.servicemanager.vo.BusinessRequestVo;

import java.util.Date;

/**
 * @author Anson
 * @date 17-8-30
 * @name 系统派单中预约单确认
 * @since 1.0.7
 */
public class BespeakSystemDistributeBusinessContext extends
        AbstractGrabApplicationContext implements SystemDistributeBusinessContext {
    private static final Logger LOGGER = LoggerFactory.getLogger(BespeakSystemDistributeBusinessContext.class);
    private final BespeakTimeRedisRestoreConvertor convertor = new BespeakTimeRedisRestoreConvertor();

    private final DriverRegisterBusRelative relative;
    private final BusPoint busPoint;

    public BespeakSystemDistributeBusinessContext(DriverRegisterBusRelative relative, BusPoint busPoint) {
        this.relative = relative;
        this.busPoint = busPoint;
    }

    @Override
    public UserRequestTypeInfo.UserRequestType getRequestType() {
        return UserRequestTypeInfo.UserRequestType.BESPEAK;
    }

    @Override
    public boolean handleSystemDistributeBusiness(BusinessRequestVo businessRequestVo, String businessId,
                                                  String driverId) throws BusinessException {
        GrabRedisBusinessWorkCacheService grabBusinessWorkCacheService = getGrabRedisBusinessWorkCacheService();
        GrabDriverRegisterBusRelativeService grabDriverRegisterBusRelativeService = this.getDriverRegisterBusRelativeServiceReference();
        BusinessServiceService businessServiceService = this.getBusinessServiceServiceReference();
        BusinessCacheService businessCacheService = this.getBusinessCacheServiceReference();
        BusinessServiceInfoService businessServiceInfoService = getBusinessServiceInfoServiceReference();
        RequestTask requestTask = businessRequestVo;
        boolean isDone = false;
        UserRequestTypeInfo userRequestInfo = requestTask.getUserRequestInfo();
        long bespeakDate = userRequestInfo.getBespeakTime();
        long requestCreateTime = userRequestInfo.getCreateTime();
        if (bespeakDate < requestCreateTime) {
            isDone = false;
            return isDone;
        }
        Date date = new Date(bespeakDate);
        String redisBespeakDate = convertor.convertCornA().convert(date);
        try {
            //判断司机是否含有了当前预约时间的预约单
            boolean isContainBespeakDate = businessServiceInfoService.isContainerDriverBespeakDate(driverId, new Date(bespeakDate));
            if (isContainBespeakDate) {
                //当前司机存在预约单子的时候
                isDone = false;
                return isDone;
            }
            //更新司机预约的日期序号
            isDone = grabBusinessWorkCacheService.addBespeakDate(driverId, redisBespeakDate);
            if (!isDone) {
                isDone = false;
                return isDone;
            }
            //创建一个预约业务
            isDone = businessServiceService.doBespeakBusiness(relative,
                    businessId, driverId, busPoint, businessRequestVo);

        } catch (Exception e) {
            isDone = false;
            LOGGER.error("[预约包车]抢单模式,司机抢单,系统派单某个司机进行派单分发->当前业务={},driverId={}", businessId, driverId);
            throw e;
        } finally {
            if (isDone) {
                //若是当前的处理结果成功，则确认当前分配的结果正确性
                //则在缓存业务中需要更新当前业务状态为开始形成的状态
                BusinessStatus beginStatus = BusinessStatus.BESPEAK_SUCCESS;
                LOGGER.info("[预约包车]抢单模式,司机抢单,系统派单某个司机进行派单分发->当前业务={},修改当前业务状态为={}", businessId, beginStatus.getCode());
                businessCacheService.updateStatus(businessId, beginStatus);
                grabBusinessWorkCacheService.doBespeakGrabBusinessSuccess(driverId);
                grabDriverRegisterBusRelativeService.doBespeakGrabBusinessSuccess(relative);
            }
        }
        return isDone;
    }
}
